package com.glodon.paas.search.controller;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glodon.paas.search.model.HitFiles;
import com.glodon.paas.search.model.QueryDocument;
import com.glodon.paas.search.model.QueryRevitStructure;
import com.glodon.paas.search.query.QueryService;
import com.glodon.paas.search.util.SearchConstants;

@Controller
public class SearchController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    @Qualifier("documentQueryService")
    private QueryService docQueryService;

    @Autowired
    @Qualifier("revitQueryService")
    private QueryService revitQueryService;

    @RequestMapping(value = "/doc", method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<HitFiles> searchDoc(QueryDocument file) {
        HitFiles hitFiles = docQueryService.query(SearchConstants.INDEX_PAAS, SearchConstants.TYPE_DOC, file);
        return new ResponseEntity<HitFiles>(hitFiles, HttpStatus.OK);
    }

    @RequestMapping(value = "/rvt", method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<HitFiles> searchRvt(QueryRevitStructure file) {
        HitFiles hitFiles = revitQueryService.query(SearchConstants.INDEX_PAAS, SearchConstants.TYPE_RVT, file);
        return new ResponseEntity<HitFiles>(hitFiles, HttpStatus.OK);
    }

    @InitBinder
    public void binder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            public void setAsText(String value) {
                try {
                    if (StringUtils.isNotEmpty(value)) {
                        setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(value));
                    } else {
                        setValue(null);
                    }
                } catch (ParseException e) {
                    logger.error("invalid \"yyyy-MM-dd HH:mm\" date format.", e);
                    setValue(null);
                }
            }

            public String getAsText() {
                if (getValue() != null) {
                    return new SimpleDateFormat("yyyy-MM-dd HH:mm").format((Date) getValue());
                } else {
                    return "";
                }
            }
        });
    }
}
